<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="referrer" content="no-referrer" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/index.css">
    <!-- <link rel="stylesheet" href="../../static/css/bootstrap.css"> -->
    <script src="../../static/javascripts/cookie.js"></script>
    <style>
        body {
            background: url(../../static/img/passport-banner.jpg);
        }

        .alert {
            position: absolute;
            width: 100%;

            text-align: center;
            display: none;
        }

        .container {
            padding-top: 60px;
            width: 302px;
            height: 200px;
            border: 1px solid #684029;
            float: right;
            margin-right: 100px;
            margin-top: 100px;
            border-radius: 10px;
            position: relative;
        }

        .container h2 {
            text-align: center;
            line-height: 25px;
            font-size: 18px;
            color: #482618;
            margin-top: -30px;
        }

        .form-group input {
            width: 270px;
            height: 38px;
            margin-left: 10px;
            margin-top: 20px;
            border: 1px solid #684029;
        }

        #btn {
            display: block;
            background: #442818;
            border-radius: 2px;
            height: 48px;
            width: 270px;
            line-height: 48px;
            text-align: center;
            font-size: 18px;
            color: #fff;
            border: 0;
            cursor: pointer;
            color: #fff;
            position: absolute;
            bottom: 10px;
            left: 10px;
        }
    </style>
</head>

<body>
    <div class="header">
        <div class="header-logo">
            <img src="https://oss.51cocoa.com/themes/site/img/logo.png" alt="">
        </div>
        <ul class="nav-box">
            <a href="index.html">首页</a>
            <a href="cake.html">蛋糕</a>
            <a href="cake.html">面包</a>
            <a href="cake.html">冰淇淋</a>
            <a href="cake.html">冻与焗/轻蛋糕</a>
            <a href="cake.html">2小时急达送</a>
        </ul>
        <div class="right-user">
            <a class="username"></a>
            <a href="loginin.html">登录</a>
            <a href="register.html">注册</a>
            <a href="cart.html">购物车</a>
        </div>
    </div>
    <div class="alert alert-success" id="login_tip">这是一个提示</div>
    <div class="container">
        <form action="javascript:void(0)" class="form-horizontal col-md-4 col-md-offset-4">
            <h2 class="form-signin-heading">请登录</h2>

            <div class="form-group">
                <input type="text" class="form-control" id="username" autocomplete="off" placeholder="请输入您的账号">
            </div>

            <div class="form-group">
                <input type="text" class="form-control" id="password" autocomplete="off" placeholder="密码">
            </div>

            <button id="btn" class="btn btn-primary btn-sm">登录</button>
        </form>
    </div>
    <script src="../../static/javascripts/ajax-promise.js"></script>
    <script src="../../static/javascripts/cookie.js"></script>
    <script>
        //业务
        //1.点击按钮发送请求
        //发送请求的方式
        //1.xhr发送请求
        //2.fetch发送请求
        //2.根据响应数据决定提示内容
        //登录信息之中存在一个token，这个token是后端经过加密处理之后的登录凭证
        //你可以利用这个凭证在后续的操作之中证明你的身份
        var btn = document.getElementById("btn")
        var username = document.getElementById("username")
        var password = document.getElementById("password")
        btn.onclick = async function () {
            var options = {
                url: "http://127.0.0.1:8888/users/login",
                type: "POST",
                data: {
                    username: username.value,
                    password: password.value
                }
            }
            let data = await ajax_promise(options)

            data = JSON.parse(data)
            console.log(data);
            switch (data.code) {
                case 0:
                    errorTip(data.mesbsage);
                    break;
                case 1:
                    successTip(data.message)
                    setTimeout(() => {
                    let res = localStorage.res;
                    localStorage.res = JSON.stringify(options.data.username) ;
                    // console.log(data);
                        location.href = "index.html"
                    }, 2000);
                    setCookie("userid", data.user.id);
                    setCookie("token", data.token, {
                        expires: 1
                    });

            }

        }
        // var login_tip = document.getElementById("login_tip")
        function errorTip(msg) {
            login_tip.style.display = "block"
            login_tip.classList.remove("alert-success")
            login_tip.classList.add("alert-danger")
            login_tip.innerHTML = msg
            setTimeout(() => {
                login_tip.style.display = "none"
            }, 2000);

        }
        function successTip(msg) {
            login_tip.classList.remove("alert-danger")
            login_tip.classList.add("alert-success")
            login_tip.style.display = "block"
            login_tip.innerHTML = msg;
            setTimeout(() => {
                login_tip.style.display = "none"
            }, 2000);
        }
    </script>

</body>

</html>